var express = require("express");
var router = express.Router();
const Result = require("../../utils/result");
const topicServices = require("../services/topic");

//添加话题
router.post("/topic", async function (req, res) {
    console.log(11111);
  const { topic } = req.body;
  await topicServices.addTopic(topic);
  res.json(Result.success());
});

//获取一条话题
router.get("/topic/:id", async function (req, res) {
  const id = req.params.id;
  const topic = await topicServices.getTopic(id);
  res.json(Result.success({ topic }));
});

//更新一条话题
router.put("/topic/:id", async function (req, res) {
  const id = req.params.id;
  const topic = req.body.topic;
  await topicServices.updateTopic(id, topic);
  res.json(Result.success());
});

//删除一条话题
router.delete("/topic/:id", async function (req, res) {
  const id = req.params.id;
  await topicServices.deleteTopic(id);
  res.json(Result.success());
});

//获取话题列表
router.get("/getList", async function (req, res) {
  const { keyword, page, size } = req.query;
  const { rows, count } = await topicServices.getTopicList(keyword, page, size);
  res.json(Result.success({ topicData: rows,count }));
});

module.exports = router;
